Keras Models

flowchart TD

A[Keras Models]

A --> B[Sequential Model]
A --> C[Functional API]
A --> D[Model Subclassing]

B --> B1[Simple stack of layers]

C --> C1[Multiple Inputs]
C --> C2[Multiple Outputs]
C --> C3[Branching]
C --> C4[Skip Connections]

D --> D1[Custom Forward Pass]
D --> D2[Research Models]
D --> D3[Dynamic Networks]
        

1. Sequential Model

Every layer has exactly one input and one output. Layers are placed in linear fashion, to create neural network
Eg: Multi-layer perceptrons (MLPs), Basic Convolutional Neural Networks (CNNs), Recurrent Neural Networks (RNNs)

flowchart LR

X[Input]

L1[Layer 1]

L2[Layer 2]

L3[Layer 3]

Y[Output]

X --> L1 --> L2 --> L3 --> Y
        

2. Functional Model

Non-linear data flow, complex layers, shared layers, and multiple inputs/outputs Supports Multiple Inputs/Outputs
Eg: Multi-input/multi-output models (e.g., a model that takes image and text inputs to produce a classification and a regression output)


FUNCTIONAL API - GRAPH STRUCTURE
                          ┌─────────┐
                     ┌───▶│ Dense 3 │───┐
┌─────────┐     ┌────┴─┐  └─────────┘   ▼  ┌─────────┐
│ INPUT 1 │────▶│      │             ┌─────▶│ OUTPUT 1│
└─────────┘     │ Dense│             │      └─────────┘
                 │   1  │             │
┌─────────┐     │      │             │      ┌─────────┐
│ INPUT 2 │────▶│      │───┐         └─────▶│ OUTPUT 2│
└─────────┘     └──────┘   │                └─────────┘
                 │         ▼
                 │    ┌─────────┐
                 └───▶│ Dense 2 │
                      └─────────┘
          

3. Model Subclassing (Research, highly custom architectures)

Provides highest level of flexibility, allowing to implement everything from scratch by subclassing the tf.keras.Model For dynamic architectures, such as models that require custom loops or conditional logic in the forward pass Supports Multiple Inputs/Outputs
Eg: Out-of-the-box research models or custom architectures like a Tree-RNN

Layers in Keras?

Layer is combination of Neurons. 1 layer will have multiple neurons
Layer is component used to build a neural network. Multiple layers are stacked together to create a neural network. Output from 1 layer is fed to other.

Types of Layers

Common Keras layer types include:
1. Dense — fully connected layers for general-purpose modeling.
2. Conv2D / Conv1D — convolutional layers for images, time series, and spatial data.
3. Pooling — downsampling layers such as MaxPool2D and AveragePool2D.
4. Dropout — regularization layers that randomly drop units during training.
5. Normalization — layers such as BatchNormalization and LayerNormalization.

1. Dense Layer

Dense(8) = Layer with 8 neurons. 8 labels
Dense(4) = Layer with 4 neurons. 4 labels
Dense(1) = Layer with 1 neuron. 1 label
Neural Network = {Dense(8) -> Dense(4) -> Dense(1)}

Layers

How Neurons Per layer and number of layers is decided

Number of input features → Determined by your dataset.
Example: Age, Salary, Experience = 3 features.
Number of neurons per layer → Chosen by the model designer (a hyperparameter).
Number of hidden layers → Also chosen by the model designer.

Create Neural Network

In Keras you mostly call functions (or constructors that look like functions). Each call creates an object — a layer, a model, or a training setting — and you chain them together.
Think of it like building with LEGO: layers.Dense(...) is one brick, keras.Sequential([...]) is the base plate that holds the stack, model.compile(...) tells Keras how to train, and model.fit(...) actually runs training.

flowchart LR
  A["1. Define model
Sequential / Functional"] --> B["2. compile()
optimizer, loss, metrics"] B --> C["3. fit()
x, y, epochs, batch_size"]

Functions

Containers

Sequential([...]), Model

Hold layers together — the neural network object

Layers

Layers.Input(shape=(...))

How much data the model expects. The shape of the input data (features).

keras.Layer.Dense(n)

Linear Regression: y = mx + b (1 feature, 1 output)
model = keras.Sequential([
  layers.Input(shape=(1,)), # 1 input feature(x)
  layers.Dense(1),          # Layer with 1 neuron(ie 1 feature).
])
MLP — binary classification: y = m1x1 + m2x2 + ... + b (n features, 1 output)
model = keras.Sequential([
  layers.Dense(64, activation="relu"),    # Layer with 64 Neurons. 64 labels(y1, y2, ... y64)
  layers.Dense(32, activation="relu"),    # Layer with 32 Neurons. 32 labels(y1, y2, ... y32)
  layers.Dense(1, activation="sigmoid"),  # Layer with 1 Neuron. 1 label(y)
])

// activation function applied to output of layer (Term.html)

Layer.Dropout(rate)


# MLP — multi-class classification: y = m1x1 + m2x2 + ... + b  (n features, K outputs)
model = keras.Sequential([
    layers.Input(shape=(784,)),                     # 784 input features (x1, x2, ... x784)
    layers.Dense(128, activation="relu"),           # Layer with 128 Neurons. 128 labels(y1, y2, ... y128)
    layers.Dropout(0.2),                            # Drop out 20% of the neurons
    layers.Dense(10, activation="softmax"),         # Layer with 10 Neurons. 10 labels(y1, y2, ... y10)
])
      

Train, Predict

compile(optimizer, loss, accuracy)

Configure the model for training. optimizer(adam), Loss(binary_crossentropy)

model.compile(
  optimizer="adam",
  loss="binary_crossentropy",
  metrics=["accuracy"],
)

fit(x, y, epochs, batch_size)

Train the model

predict(x)

Make predictions with the model

predictions = model.predict(X_validation, verbose=0)

evaluate()

computes the performance metrics (like loss and accuracy) of a trained model

loss, accuracy = model.evaluate(X, y, verbose=0)

Optimizer / loss

SGD, Adam, RMSprop, "mse"
Passed into compile() — not layers

What Sequential cannot do easily

Use the Functional API (above) when you need multiple inputs, multiple outputs, shared layers, or skip connections (ResNet, U-Net, two-tower models). Use Model subclassing for fully custom forward passes (GANs, neural ODEs, dynamic graphs).